﻿using RadarMonitor.com.ums86.gd;
using RadarMonitor.Converter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;


namespace RadarMonitor.Alarm
{
    /// <summary>
    /// 短信报警
    /// 手机号列表存在phones.txt内,每行一个手机号
    /// </summary>
    public class SmsAlarm
    {
        private static SourceCodeConverter sourceCodeConverter = new SourceCodeConverter();
        private static ResultCodeConverter resultCodeConverter = new ResultCodeConverter();
        public static string SN = "239441";
        public static string UID = "wh_qxj";
        //public static string PWD = "1q2w3eQXJ#";
        public static string PWD = "qxj123";

        public static void Alarm(RadarMonitorSource source, RadarMonitorResultCode code, string msg) {
            if (RadarMonitor.Setting.Setting.GetConfig().SMS.enable == false)
            {
                return;
            }
            string sourceStr = sourceCodeConverter.Convert(source, typeof(string), null, null) as string;
            string resultStr = resultCodeConverter.Convert(code, typeof(string), null, null) as string;
            string content = $"报警源：{sourceStr}\n报警类型：{resultStr}";
            if (msg != null)
            {
                content += $"\n备注：{msg}";
            }
            //读取手机号
            List<string> phones = new List<string>();
            try
            {
                using (var rd = new StreamReader(@".\phones.txt"))
                {
                    do
                    {
                        var phone = rd.ReadLine();
                        if (phone == null)
                            break;
                        phones.Add(phone);
                    } while (true);
                }
            }
            catch { }
           
            foreach(var phone in phones)
            {
                try {
                    Send("监控报警", phone, content);
                } catch { }
            }
        }


        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="telephone">手机号码</param>
        /// <param name="msg">内容</param>
        /// <returns></returns>
        public static string Send(string id, string telephone, string msg)
        {
            if (string.IsNullOrEmpty(telephone.Trim()) || telephone.Trim().Length != 11)
                throw new Exception("手机格式不正确");

            try
            {
                Sms sms = new Sms();
                string res = sms.CallSms(SN, UID, PWD, msg, telephone, "12345678900987654321", null, "1", null, null, null);
                return res;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 查询短信发送状态
        /// </summary>
        /// <param name="msgID"></param>
        /// <returns></returns>
        public static IList<string[]> Receipt()
        {
            try
            {
                Sms sms = new Sms();
                string result = sms.Report(SN, UID, PWD);
                IList<string[]> list = result.Split('&')[1].Split('=')[1].Split(';').Where(x => !string.IsNullOrEmpty(x)).Select(x => x.Split(',')).ToList();

                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

